﻿//文件指定编译编码UTF-8
#pragma execution_character_set("utf-8")
#include "servicecontroller.h"
#include "ui_servicecontroller.h"

#include <QFileDialog>
#include <QSettings>
#include <QDebug>
#include <QMessageBox>
#include <QProcess>

ServiceController::ServiceController(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::ServiceController)
{
    ui->setupUi(this);
    init_ServiceControlParam();
    ui->serviceSettingBtn->setVisible(serviceSetting);
    connect(&m_timer,&QTimer::timeout,this,&ServiceController::getServerState);
    m_timer.start(1000);
}

ServiceController::~ServiceController()
{
    seri_ServiceControlParam();
    m_timer.stop();
    delete ui;
}

void ServiceController::getServerState()
{
    serverState = deviceOperator.getServiceState(ui->serviceName->text().toStdString().c_str());
    ui->serviceAddDelBtn->setText(serverState == -1 ? QString("安装") : QString("卸载"));
    ui->serviceStartStopBtn->setText(serverState == SERVICE_RUNNING ? QString("停止") : QString("启动"));
    ui->serviceRestartBtn->setEnabled(serverState == SERVICE_RUNNING);
    ui->serviceStartStopBtn->setEnabled(serverState != -1);
    //    ui->serviceName->setEnabled(serverState == -1);
    ui->serviceDescription->setEnabled(serverState == -1);
    switch (serverState) {
    case -1:
        ui->servoiceState->setText(QString("服务未安装！"));
        break;
    case SERVICE_RUNNING:
        ui->servoiceState->setText(QString("服务已启动！"));
        break;
    case SERVICE_STOPPED:
        ui->servoiceState->setText(QString("服务已停止！"));
        break;
    }
}

void ServiceController::init_ServiceControlParam()
{
    QSettings settings(QCoreApplication::applicationDirPath()+"/"+QCoreApplication::applicationName()+".ini",QSettings::IniFormat);
    settings.setIniCodec("UTF-8");

    if(settings.value("Title/name").isNull()) {
        setWindowTitle("服务管理程序");
        settings.setValue("Title/name",windowTitle());
    }else
        setWindowTitle(settings.value("Title/name").toString());

    if(settings.value("Service/serverName").isNull()) {
        ui->serviceName->setText("MyService");
        settings.setValue("Service/serverName",ui->serviceName->text());
    }else
        ui->serviceName->setText(settings.value("Service/serverName").toString());

    if(settings.value("Service/serverDescription").isNull()) {
        ui->serviceDescription->setText("我的服务");
        settings.setValue("Service/serverDescription",ui->serviceDescription->text());
    }else
        ui->serviceDescription->setText(settings.value("Service/serverDescription").toString());

    if(settings.value("Service/serverPath").isNull()) {
        servicePath="";
        settings.setValue("Service/serverPath",servicePath);
    }else
        servicePath=settings.value("Service/serverPath").toString();

    if(settings.value("Service/serviceSetting").isNull()) {
        serviceSetting=false;
        settings.setValue("Service/serviceSetting",serviceSetting);
    }else
        serviceSetting=settings.value("Service/serviceSetting").toBool();

    if(settings.value("Service/serviceSettingPath").isNull()) {
        serviceSettingPath="";
        settings.setValue("Service/serviceSettingPath",serviceSettingPath);
    }else
        serviceSettingPath=settings.value("Service/serviceSettingPath").toString();
}

void ServiceController::seri_ServiceControlParam()
{
    qDebug() << QCoreApplication::applicationDirPath()+"/"+QCoreApplication::applicationName()+".ini";
    QSettings settings(QCoreApplication::applicationDirPath()+"/"+QCoreApplication::applicationName()+".ini",QSettings::IniFormat);
    settings.setIniCodec("UTF-8");

    settings.setValue("Service/serverName",ui->serviceName->text());
    settings.setValue("Service/serverDescription",ui->serviceDescription->text());
}

void ServiceController::on_serviceAddDelBtn_clicked()
{
    QString serverName,serverDescription;
    serverName = ui->serviceName->text();
    serverDescription = ui->serviceDescription->text();
    if(serverState == -1){
        if(serverDescription.isEmpty())
            serverDescription = serverName;
        if(!serverName.isEmpty() && !servicePath.isEmpty()){
            deviceOperator.InstallServiceInterface(serverName.toStdString().c_str(),
                                                   serverDescription.toStdWString().c_str(),
                                                   QString("%1/%2").arg(QCoreApplication::applicationDirPath()).arg(servicePath).toStdString().c_str());
            qDebug() << QString("%1/%2").arg(QCoreApplication::applicationDirPath()).arg(servicePath).toStdString().c_str();
        }else{
            QMessageBox::information(this,"温馨提示","服务名称和服务路径不可为空！");
        }
    }else{
        deviceOperator.UninstallServiceInterface(serverName.toStdString().c_str());
    }
}

void ServiceController::on_serviceStartStopBtn_clicked()
{
    if(serverState == SERVICE_STOPPED){
        deviceOperator.StartServiceInterface(ui->serviceName->text().toStdString().c_str());
    }else if(serverState == SERVICE_RUNNING){
        deviceOperator.StopServiceInterface(ui->serviceName->text().toStdString().c_str());
    }
}

void ServiceController::on_serviceRestartBtn_clicked()
{
    deviceOperator.StopServiceInterface(ui->serviceName->text().toStdString().c_str());
    deviceOperator.StartServiceInterface(ui->serviceName->text().toStdString().c_str());
}

void ServiceController::on_serviceSettingBtn_clicked()
{
    QString path = QString("%1/%2").arg(QCoreApplication::applicationDirPath()).arg(serviceSettingPath);
    QProcess *process = new QProcess(this);
    process->start(path);
}
